import 'package:api/pages/provider/provider/model/home_card_model.dart';
import 'package:api/pages/provider/provider/model/home_item_model.dart';
import 'package:api/pages/provider/provider/model/home_tab_model.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class HomeItemPage extends StatefulWidget {
  final TabBean tabBean;
  const HomeItemPage({Key key, this.tabBean}) : super(key: key);
  @override
  _HomeItemPageState createState() => _HomeItemPageState();
}

class _HomeItemPageState extends State<HomeItemPage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Future.delayed(Duration(milliseconds: 2000), () {
      context.read<HomeItemModel>().request();
    });
  }

  @override
  void dispose() {
    super.dispose();
    // print("页面销毁 ${widget.tabBean.title}");
  }

  @override
  Widget build(BuildContext context) {
    List<ListItemBeam> list = context.watch<HomeItemModel>().tabList;
    if (list.length == 0) {
      return Center(
        child: Text("加载中..."),
      );
    }
    return ListView.builder(
        itemCount: list.length,
        itemBuilder: (BuildContext context, int index) {
          return buildItem(index, list[index]);
        });
  }

  buildItem(int index, ListItemBeam bean) {
    return Container(
      color: Colors.white,
      margin: EdgeInsets.only(top: 8),
      padding: EdgeInsets.all(10),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Image.asset(
            bean.img,
            width: 100,
          ),
          Expanded(
            child: buildContainer(bean),
          ),
        ],
      ),
    );
  }

  Container buildContainer(ListItemBeam bean) {
    return Container(
      padding: EdgeInsets.all(10),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Text("${bean.title}"),
          SizedBox(height: 16),
          Row(children: [
            Expanded(child: Text("价格 ${bean.price}")),
            GestureDetector(
              onTap: () {
                if (bean.isAdd) {
                  bean.isAdd = false;
                  context.read<HomeCardModel>().removeCardList(bean);
                } else {
                  bean.isAdd = true;
                  context.read<HomeCardModel>().addCardList(bean);
                }
              },
              child: Container(
                alignment: Alignment.center,
                padding: EdgeInsets.only(left: 12, right: 12),
                decoration: BoxDecoration(
                  color: bean.isAdd?Colors.yellow:Colors.blueGrey,
                  borderRadius: BorderRadius.all(
                    Radius.circular(30),
                  ),
                ),
                width: 120,
                child: Text(bean.isAdd ? "移除购物车" : "加入购物车",
                    style: TextStyle(color: Colors.white)),
              ),
            )
          ]),
        ],
      ),
    );
  }
}
